/**
 * @FileName: ThemeController.java
 * @Author
 * @Description:
 * @Date 2016年1月4日 下午5:53:11
 * @CopyRight ZTE Corporation
 */
package cn.hpclub.server.controller.client;

import org.json.JSONObject;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.jfinal.core.Controller;
import com.jfinal.kit.StrKit;
import com.jfinal.plugin.activerecord.Model;

import cn.hpclub.server.constant.ClientApiConstant;
import cn.hpclub.server.model.DeviceVersion;
import cn.hpclub.server.restful.API;

@API("/SoapApiServer/Version")
public class SoapApiServerController<M extends Model<M>> extends Controller implements ClientApiConstant{
    private static Logger log = LoggerFactory.getLogger(SoapApiServerController.class);

    /**
     * @Title: CheckVersionForIos
     * @Description:3.3.2.10.1 检查版本信息
     */
    public void CheckVersionForIos(){
        String type = getPara("type");
        String version = getPara("version");
        String sysFlag = getPara("sysflage");
        JSONObject jo = null;
        try{
            DeviceVersion deviceVersion = DeviceVersion.dao.getAavailableVersion(version, type, sysFlag);
            if(null == deviceVersion){
                /* 无可升级版本 */
                jo = getNoVersionResponse();
            } else{
                /* 有可升级版本 */
                jo = getHasVersionResponse(deviceVersion);
            }
        }
        catch(Exception e){
            log.error(e.getMessage());
            jo = getErrorResponse();
        }
        renderJson(jo.toString());
    }

    /**
     * @Title: responseError
     * @Description:获取服务器处理异常回复
     * @return JSONObject
     */

    private JSONObject getErrorResponse(){
        JSONObject jo = new JSONObject().put("ResultCode", ClientApiConstant.ResultCode.SERVER_EXCEPTION)
                .put("Message", "服务器处理异常").put("Data", "");
        return jo;
    }

    /**
     * @Title: responseHasVersion
     * @Description:获取版本信息回复
     * @param deviceVersion
     *            void
     */

    private JSONObject getHasVersionResponse(DeviceVersion deviceVersion){
        JSONObject jo = new JSONObject().put("ResultCode", ClientApiConstant.ResultCode.SUCCESS_CODE).put("Message",
                                                                                                          "有新版本可供升级");
        JSONObject data = new JSONObject();
        String url = deviceVersion.getStr("downloadPath");
        String apkUrl = url;
        if(!StrKit.isBlank(url)){
            if(url.toLowerCase().startsWith("http://") || url.toLowerCase().startsWith("https://")){
                apkUrl = url;
            } else{
                apkUrl = ActivitiesController.hostPrefix + url;
            }
        }
        data.put("Type", deviceVersion.getInt("type")).put("appName", deviceVersion.getStr("appName"))
                .put("version", deviceVersion.getLong("version")).put("fileName", deviceVersion.getStr("fileName"))
                .put("fileSize", deviceVersion.getStr("fileSize")).put("downloadPath", apkUrl)
                .put("description", deviceVersion.getStr("description"))
                .put("isForceUpdate", deviceVersion.getInt("isForceUpdate")).put("status", 1)
                .put("md5", deviceVersion.getStr("md5"));
        jo.put("Data", data);
        return jo;
    }

    /**
     * @Title: responseNoVersion
     * @Description:获取无版本可更新回复 void
     */

    private JSONObject getNoVersionResponse(){
        JSONObject jo = new JSONObject().put("ResultCode", ClientApiConstant.ResultCode.SUCCESS_CODE).put("Message",
                                                                                                          "当前已是最新版本");
        jo.put("Data", new JSONObject().put("status", 0));
        return jo;
    }
}
